package com.example.work.check;


import com.example.work.entiy.DivisionAssetInfoWithIpPorts;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 模拟数据库服务类
 * 用于模拟从数据库查询division_asset_info表的数据
 */
public class MockDatabaseService {
    
    private List<DivisionAssetInfoWithIpPorts> mockData;
    private final Random random = new Random();
    
    public MockDatabaseService() {
        initializeMockData();
    }
    
    /**
     * 初始化模拟数据
     */
    private void initializeMockData() {
        mockData = new ArrayList<>();
        
        // 模拟一些数据库中已存在的资产数据
        
        // 数据库资产
        mockData.add(createMockAsset(1, "AS-DB-001", "192.168.1.10", "3306", "0702", 
                "主数据库服务器", "MySQL", "8.0"));
        mockData.add(createMockAsset(2, "AS-DB-002", "192.168.1.11", "3306", "0702", 
                "备份数据库服务器", "MySQL", "8.0"));
        
        // 服务器资产
        mockData.add(createMockAsset(3, "AS-SRV-001", "192.168.1.20", "22", "0101", 
                "Web服务器", "Dell", "PowerEdge R740"));
        mockData.add(createMockAsset(4, "AS-SRV-002", "192.168.1.21", "80", "0101", 
                "应用服务器", "HP", "ProLiant DL380"));
        
        // 负载均衡资产
        mockData.add(createMockAsset(5, "AS-LB-001", "192.168.1.30", "443", "0106", 
                "主负载均衡器", "F5", "BIG-IP"));
        
        // Web资产
        mockData.add(createMockAsset(6, "AS-WEB-001", "192.168.1.40", "8080", "0699", 
                "业务系统门户", "Tomcat", "9.0"));
        
        // 中间件资产
        mockData.add(createMockAsset(7, "AS-MW-001", "192.168.1.50", "9092", "0703", 
                "消息中间件", "Apache Kafka", "2.8.0"));
        
        // 其他资产
        mockData.add(createMockAsset(8, "AS-OTHER-001", "192.168.1.60", "6379", "0799", 
                "缓存服务器", "Redis", "6.2"));
        
        System.out.println("模拟数据库初始化完成，共 " + mockData.size() + " 条记录");
    }
    
    /**
     * 创建模拟资产数据
     */
    private DivisionAssetInfoWithIpPorts createMockAsset(int id, String groupAssetId, String assetIp, 
                                            String port, String assetTypeCode, 
                                            String assetName, String manufacturer, String model) {
        DivisionAssetInfoWithIpPorts asset = new DivisionAssetInfoWithIpPorts();
        
       /* asset.setId(id);
        asset.setRalaId(random.nextInt(1000) + 1000);
        asset.setGroupAssetId(groupAssetId);
        asset.setDivisionAssetId("DIV-" + groupAssetId);
        asset.setIspCode("CMCC");
        asset.setOrgCode("440000");
        asset.setAssetIp(assetIp);
        asset.setPort(port);
        asset.setIpType(0); // IPv4
        asset.setExpose(0); // 不可从互联网访问
        asset.setNetPosition("内网");
        asset.setAssetName(assetName);
        asset.setAssetType(1);
        asset.setReportAssetType(assetTypeCode);
        asset.setStatus(1); // 在网
        asset.setNetworkType(0); // 内网
        asset.setRegion("广东省广州市");
        asset.setManageTag(1); // 业务地址
        asset.setAssetTag("PROD");
        asset.setAssetModel(model);
        asset.setAssetVersion("1.0");
        asset.setManufacturer(manufacturer);
        asset.setBrandName(manufacturer);
        asset.setBrandRegion("国外");
        asset.setOpenSource(0);
        asset.setSystemName("核心业务系统");
        asset.setObejectName("二级系统");
        asset.setObejectLevel("二级");
        asset.setNetworkUnitType("服务器");
        asset.setFoundType(1);
        asset.setFoundTime("2024-01-01 10:00:00");
        asset.setCreateTime("2024-01-01 10:00:00");
        asset.setUpdateTime("2024-01-01 10:00:00");
        asset.setImportant(0);
        asset.setManagerName("张三");
        asset.setManagerMobile("13800138000");
        */
        return asset;
    }
    
    /**
     * 获取所有资产数据
     */
    public List<DivisionAssetInfoWithIpPorts> getAllAssets() {
        return new ArrayList<>();
//        return new ArrayList<>(mockData)
    }
    
    /**
     * 按条件查询资产
     */
    public List<DivisionAssetInfoWithIpPorts> findAssetsByCondition(String assetIp, String port, String assetTypeCode) {
        return mockData.stream()
                .filter(asset -> assetIp.equals(asset.getAssetIp()) && 
                               port.equals(asset.getPorts()) &&
                               assetTypeCode.equals(asset.getReportAssetType()))
                .collect(java.util.stream.Collectors.toList());
    }
    
    /**
     * 模拟批量插入操作
     */
    public int batchInsert(List<DivisionAssetInfoWithIpPorts> assets) {
        System.out.println("模拟执行批量插入操作，共 " + assets.size() + " 条记录");
        
        for (DivisionAssetInfoWithIpPorts asset : assets) {
            // 模拟生成自增主key
            asset.setId(mockData.size() + 1);
            mockData.add(asset);
            
            System.out.println("插入记录: ID=" + asset.getId() + 
                    ", IP=" + asset.getAssetIp() + 
                    ", 端口=" + asset.getPorts() +
                    ", 类型=" + asset.getReportAssetType() + 
                    ", 名称=" + asset.getAssetName());
        }
        
        return assets.size();
    }
    
/*    *//**
     * 打印数据库中的所有数据（调试用）
     *//*
    public void printAllAssets() {
        System.out.println("数据库中的所有资产:");
        for (DivisionAssetInfoWithIpPorts asset : mockData) {
            System.out.println("ID: " + asset.getId() + 
                    ", 匹配关键字: " + asset.createMatchKey() + 
                    ", 名称: " + asset.getAssetName());
        }
    }*/
    
    /**
     * 清空所有数据（仅用于测试）
     */
    public void clearAllData() {
        mockData.clear();
        System.out.println("数据库数据已清空");
    }
    
    /**
     * 添加测试数据（用于验证匹配逻辑）
     */
    public void addTestDataForMatching() {
        // 添加一些与Excel中可能存在的数据一致的记录
        // 这些数据将在比对时被匹配到
        mockData.add(createMockAsset(100, "EXCEL-DB-001", "10.0.0.1", "3306", "0702", 
                "Excel中的数据库", "Oracle", "19c"));
        mockData.add(createMockAsset(101, "EXCEL-SRV-001", "10.0.0.2", "22", "0101", 
                "Excel中的服务器", "IBM", "x3650"));
    }
}